//
//  GTLocation.m
//  SampleApp2
//
//  Created by GXHC on 2023/6/28.
//

#import <CoreLocation/CoreLocation.h>
#import "GTLocation.h"
@interface GTLocation ()<CLLocationManagerDelegate>
@property (nonatomic, strong, readwrite) CLLocationManager *manager;
@end

@implementation GTLocation
+ (GTLocation *)locationManager {
    static GTLocation *location;
    static dispatch_once_t onceToken;

    dispatch_once(&onceToken, ^{
        location = [[GTLocation alloc]init];
    });
    return location;
}

- (instancetype)init {
    self = [super init];

    if (self) {
        self.manager = [[CLLocationManager alloc]init];
        self.manager.delegate = self;
    }

    return self;
}

#pragma mark - public
- (void)checkLocationAuthorization {
    if (![CLLocationManager locationServicesEnabled]) {
    }

    if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined) {
        [self.manager requestWhenInUseAuthorization];
    }
}


#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
    if (status == kCLAuthorizationStatusAuthorizedWhenInUse) {
        [self.manager startUpdatingLocation];
    } else if (status == kCLAuthorizationStatusDenied) {
    }
}

- (void)locationManager:(CLLocationManager *)manager
     didUpdateLocations:(NSArray<CLLocation *> *)locations {
	CLLocation *location = [locations firstObject];
	CLGeocoder *coder = [[CLGeocoder alloc]init];
	[coder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
			NSLog(@"%@", placemarks);
	}];
	[self.manager stopUpdatingLocation];
}

@end
